import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class GraphicsPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	
	private Timer t;
	private Player c;
	private Player c2;
	
	int player1ctrls[] = new int[4];
	int player2ctrls[] = new int[4];
	
	public static void main(String[] e)
	{
		JFrame frame = new JFrame();
		GraphicsPanel ex = new GraphicsPanel(frame);	
		
		frame.setSize(1024, 720);
		frame.getContentPane().setBackground(Color.BLACK);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(ex);
		 
		frame.setVisible(true);
		ex.setVisible(true);
	}
	
	public GraphicsPanel(JFrame frame)
	{
		super();
		
		player1ctrls[0] = KeyEvent.VK_W;
		player1ctrls[1] = KeyEvent.VK_S;
		player1ctrls[2] = KeyEvent.VK_D;
		player1ctrls[3] = KeyEvent.VK_A;
		
		player2ctrls[0] = KeyEvent.VK_UP;
		player2ctrls[1] = KeyEvent.VK_DOWN;
		player2ctrls[2] = KeyEvent.VK_RIGHT;
		player2ctrls[3] = KeyEvent.VK_LEFT;
		
		c = new PlayerChar(player1ctrls, Color.RED, 0, 0);
		c2 = new PlayerChar(player2ctrls, Color.GREEN, 1024 - 66, 720 - 150);
		
		System.out.println("2");
		
		t = new Timer(10, this);
		t.addActionListener(c);
		this.addKeyListener(c);
		frame.addKeyListener(c);
		t.addActionListener(c2);
		this.addKeyListener(c2);
		frame.addKeyListener(c2);
		
		this.setBackground(Color.BLUE);
		t.start();
	}

	@Override
	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D)g;
		super.paint(g2d);
		g2d.setColor(c.getColor());
		g2d.fillRect(c.getX(), c.getY(), 100, 100);
		
		g2d.setColor(c2.getColor());
		g2d.fillOval(c2.getX(), c2.getY(), 50, 100);

		g2d.dispose();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		repaint();
	}
}
